package com.yehui.algorithm.sword;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 输入一个正整数数组，把数组里所有数字拼接起来排成一个数，打印能拼接出的所有数字中最小的一个。
 * 例如输入数组{3，32，321}，则打印出这三个数字能排成的最小数字为321323
 * Created by XuChunH on 2016/9/18.
 */
public class PrintMinNumber {
    public String printMinNumber(int [] numbers) {
        String result = "";
        if(numbers == null || numbers.length == 0){
            return result;
        }
        String[] array = new String[numbers.length];
        for (int i = 0; i < numbers.length; i++) {
            array[i] = numbers[i] + "";
        }
        Arrays.sort(array, new Comparator<String>(){
            @Override
            public int compare(String o1, String o2) {
                String temp1 = o1 + o2;
                String temp2 = o2 + o1;
                return temp1.compareTo(temp2);
            }
        });
        for (String s : array) {
            result += s;
        }
        return result;
    }
}
